package com.tuvi.nhamthin.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.tuvi.nhamthin.R;
import com.tuvi.nhamthin.activity.About;
import com.tuvi.nhamthin.activity.Giap;

public class UIMainScreen extends View
implements  View.OnTouchListener
{
	private static final int HEIGHT_GIAP = 45;
	private static final int HEIGHT_STAND = 480;
	private static final int WIDTH_GIAP = 55;
	private static final int WIDTH_STAND = 320;
	private static final int HEIGHT_ABOUT = 20;
	private static final int WIDTH_ABOUT = 20;
	private int height;
	private Context mContext;
	private Map<String, Rect> mapRectGiap = new HashMap();
	private int width;

	public UIMainScreen(Context paramContext, int width, int height)
	{
		super(paramContext);
		this.mContext = paramContext;
		this.width = width;
		this.height = height;
		this.setBackgroundResource(R.drawable.tuvi_bg);
	}

	private void drawGiap(Canvas paramCanvas, int paramInt1, int paramInt2, int paramInt3, int id)
	{
		drawShape(paramCanvas, this.mContext.getResources().getDrawable(paramInt1), getRectGiap(id, paramInt2, paramInt3));
	}

	private void drawShape(Canvas paramCanvas, Drawable paramDrawable, Rect paramRect)
	{
		paramDrawable.setBounds(paramRect);
		paramDrawable.draw(paramCanvas);
	}

	private Bitmap getMainScreenBM()
	{
		Drawable localDrawable = this.mContext.getResources().getDrawable(R.drawable.main_screen);
		Bitmap localBitmap = Bitmap.createBitmap(UIMainScreen.WIDTH_STAND, UIMainScreen.HEIGHT_STAND, Bitmap.Config.ARGB_8888);
		Canvas localCanvas = new Canvas(localBitmap);
		localDrawable.setBounds(0, 0, UIMainScreen.WIDTH_STAND, UIMainScreen.HEIGHT_STAND);
		localDrawable.draw(localCanvas);
		
		drawGiap(localCanvas, R.drawable.ty, UIConfig.TY_X, UIConfig.TY_Y, UIConfig.TY_ID);
		drawGiap(localCanvas, R.drawable.suu, UIConfig.SUU_X, UIConfig.SUU_Y, UIConfig.SUU_ID);
		drawGiap(localCanvas, R.drawable.dan, UIConfig.DAN_X, UIConfig.DAN_Y, UIConfig.DAN_ID);
		drawGiap(localCanvas, R.drawable.mao, UIConfig.MAO_X, UIConfig.MAO_Y, UIConfig.MAO_ID);
		drawGiap(localCanvas, R.drawable.thin, UIConfig.THIN_X, UIConfig.THIN_Y, UIConfig.THIN_ID);
		drawGiap(localCanvas, R.drawable.tyj, UIConfig.TYJ_X, UIConfig.TYJ_Y, UIConfig.TYJ_ID);
		drawGiap(localCanvas, R.drawable.ngo, UIConfig.NGO_X, UIConfig.NGO_Y, UIConfig.NGO_ID);
		drawGiap(localCanvas, R.drawable.mui, UIConfig.MUI_X, UIConfig.MUI_Y, UIConfig.MUI_ID);
		drawGiap(localCanvas, R.drawable.than, UIConfig.THAN_X, UIConfig.THAN_Y, UIConfig.THAN_ID);
		drawGiap(localCanvas, R.drawable.dau, UIConfig.DAU_X, UIConfig.DAU_Y, UIConfig.DAU_ID);
		drawGiap(localCanvas, R.drawable.tuat, UIConfig.TUAT_X, UIConfig.TUAT_Y, UIConfig.TUAT_ID);
		drawGiap(localCanvas, R.drawable.hoi, UIConfig.HOI_X, UIConfig.HOI_Y, UIConfig.HOI_ID);
		
		drawAbout(localCanvas, R.drawable.about, WIDTH_STAND -40, HEIGHT_STAND -60, UIConfig.ABOUT_ID);
		
		//drawFix(localCanvas, R.drawable.main_screen, WIDTH_STAND, HEIGHT_STAND, 14);
		return localBitmap;
	}
	/*private void drawFix(Canvas paramCanvas, int paramInt1, int paramInt2, int paramInt3, int id){
		drawShape(paramCanvas, this.mContext.getResources().getDrawable(paramInt1), getRectFix(id, paramInt2, paramInt3));
	}
	private Rect getRectFix(int id, int paramInt1, int paramInt2){
		Rect localRect = new Rect(0, paramInt2, paramInt1 + 100, paramInt2 + 50);
		this.mapRectGiap.put(String.valueOf(id), localRect);
		return localRect;
	}
	*/
	private void drawAbout(Canvas paramCanvas, int paramInt1, int paramInt2, int paramInt3, int id){
		drawShape(paramCanvas, this.mContext.getResources().getDrawable(paramInt1), getRectAbout(id, paramInt2, paramInt3));
	}
	private Rect getRectAbout(int id, int paramInt1, int paramInt2){
		Rect localRect = new Rect(paramInt1, paramInt2, paramInt1 + WIDTH_ABOUT, paramInt2 + HEIGHT_ABOUT);
		this.mapRectGiap.put(String.valueOf(id), localRect);
		return localRect;
	}
	private Rect getRectGiap(int id, int paramInt1, int paramInt2)
	{
		Rect localRect = new Rect(paramInt1, paramInt2, paramInt1 + WIDTH_GIAP, paramInt2 + HEIGHT_GIAP);
		this.mapRectGiap.put(String.valueOf(id), localRect);
		return localRect;
	}
	@Override
	protected void onDraw(Canvas paramCanvas)
	{
		Bitmap localBitmap = getMainScreenBM();
		Rect localRect2 = new Rect(0, 0, 320, 480);
		Rect localRect1 = new Rect(0, 0, this.width, (int)(1.5D * this.width));
		Paint localPaint = new Paint();
		localPaint.setColor(-16776961);
		paramCanvas.drawBitmap(localBitmap, localRect2, localRect1, localPaint);
	}
	@Override
	public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
	{
		float f1 = paramMotionEvent.getX();
		float f2 = paramMotionEvent.getY();
		f1 = 320.0F * f1 / this.width;
		float f3 = (float)(480.0F * f2 / (1.5D * this.width));
		Iterator<String> localIterator = this.mapRectGiap.keySet().iterator();
		Object localObject = "";
		while (localIterator.hasNext()){
			String str = (String)localIterator.next();
			if (!((Rect)this.mapRectGiap.get(str)).contains((int)f1, (int)f3))
				continue;
			localObject = str;
			break;
		}
		try{
			int tuoi = Integer.parseInt((String) localObject);
			if(tuoi >= UIConfig.TY_ID && tuoi <= UIConfig.HOI_ID){
				Intent i = new Intent(this.mContext, Giap.class);
				i.putExtra("tuoi", tuoi);
				mContext.startActivity(i);
				//((Activity) mContext).overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
			}else{
				if(tuoi == UIConfig.ABOUT_ID){
					Intent i = new Intent(this.mContext, About.class);
					mContext.startActivity(i);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
		return false;
	}
}